package com.sailfish.springbootdemo.service.db4;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.dao.db4.ApproveH3cDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db4.ApproveH3c;
import com.sailfish.springbootdemo.utils.HttpUtil;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ApproveH3cServiceImpl implements ApproveH3cService {

    @Autowired
    ApproveH3cDao approveH3cDao;

    @Value("${personal.config.toDo.addOrUpToDoUrl}")
    private String addOrUpToDoUrl;
    @Value("${personal.config.toDo.sysid}")
    private String sysid;
    @Value("${personal.config.toDo.syspwd}")
    private String syspwd;

    @Override
    public Result insertOrUpdate(String data) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            JSONObject jsonObject = JSONObject.parseObject(data);
            String subject = jsonObject.getString("subject");
            String adduserid = jsonObject.getString("adduserid");
            String addusername = jsonObject.getString("addusername");
            String applytime = StringUtils.isBlank(jsonObject.getString("applytime")) ? sdf.format(new Date()) : jsonObject.getString("applytime");
            String authorid = jsonObject.getString("authorid");
            String authorname = jsonObject.getString("authorname");
            String url = jsonObject.getString("url");
            String appid = jsonObject.getString("appid");
            String systemid = "IDCM";
            String processid = jsonObject.getString("processid");
            String processname = jsonObject.getString("processname");
            String docunid = jsonObject.getString("docunid");
            String status = jsonObject.getString("status");
            String enduserid = jsonObject.getString("enduserid");
            String assigner = jsonObject.getString("assigner");
            String nodeid = jsonObject.getString("nodeid");
            String nodename = jsonObject.getString("nodename");
            String timestamp = StringUtils.isBlank(jsonObject.getString("timestamp")) ? sdf.format(new Date()) : jsonObject.getString("timestamp");
            String accepttype = jsonObject.getString("accepttype");//1：PC  2:只有移动   3：都有
            String redirecturl = jsonObject.getString("redirecturl");
            Integer isbatch = 0;
            String todopcurl = jsonObject.getString("todopcurl");
            String ispcdetail = jsonObject.getString("ispcdetail") == null ? "0" : jsonObject.getString("ispcdetail");
            ;
            String buttonvalue = jsonObject.getString("buttonvalue") == null ? "点击详情" : jsonObject.getString("buttonvalue");
            Integer languageenv = 1;
            String usernodeinfo = jsonObject.getString("usernodeinfo");

            if (StringUtils.isBlank(subject) || StringUtils.isBlank(adduserid) || StringUtils.isBlank(addusername)
                    || StringUtils.isBlank(authorname) || StringUtils.isBlank(url) || StringUtils.isBlank(authorid)
                    || StringUtils.isBlank(processid) || StringUtils.isBlank(processname) || StringUtils.isBlank(docunid)
                    || StringUtils.isBlank(status) || StringUtils.isBlank(enduserid) || StringUtils.isBlank(nodeid)
                    || StringUtils.isBlank(nodename) || StringUtils.isBlank(timestamp)|| StringUtils.isBlank(accepttype)) {
                return ResultUtil.error(500, "有必填项未输入", "", null);
            }
            ApproveH3c approveH3c = new ApproveH3c();
            approveH3c.setSubject(subject);
            approveH3c.setAdduserid(adduserid);
            approveH3c.setAddusername(addusername);
            approveH3c.setApplytime(applytime);
            approveH3c.setAuthorid(authorid);
            approveH3c.setAuthorname(authorname);
            approveH3c.setUrl(url);
            approveH3c.setSystemid(systemid);
            approveH3c.setAppid(appid);
            approveH3c.setProcessid(processid);
            approveH3c.setProcessname(processname);
            approveH3c.setDocunid(docunid);
            approveH3c.setStatus(status);
            approveH3c.setEnduserid(enduserid);
            approveH3c.setAssigner(assigner);
            approveH3c.setNodeid(nodeid);
            approveH3c.setNodename(nodename);
            approveH3c.setTimestamp(timestamp);
            approveH3c.setAccepttype(accepttype);
            approveH3c.setRedirecturl(redirecturl);
            approveH3c.setIsbatch(isbatch);
            approveH3c.setTodopcurl(todopcurl);
            approveH3c.setIspcdetail(ispcdetail);
            approveH3c.setButtonvalue(buttonvalue);
            approveH3c.setLanguageenv(languageenv);
            approveH3c.setUsernodeinfo(usernodeinfo);

            ApproveH3c approveH3c1 = approveH3cDao.getById(systemid, processid, docunid);
            int flag = 0;
            if (approveH3c1 != null) {
                flag = approveH3cDao.update(approveH3c);
            } else {
                flag = approveH3cDao.insert(approveH3c);
            }
            if (flag > 0) {
                JSONObject params = new JSONObject();
                params.put("systemId", systemid);
                params.put("appId", appid);
                params.put("docunId", docunid);
                params.put("subject", subject);
                params.put("nodeName", nodename);
                params.put("nodeId", nodeid);
                params.put("addUserId", adduserid);
                params.put("addUserName", addusername);
                params.put("applyTime", applytime);
                params.put("authorId", authorid);
                params.put("authorName", authorname);
                params.put("url", url);
                params.put("processId", processid);
                params.put("processName", processname);
                params.put("endUserId", enduserid);
                params.put("status", status);
                params.put("timeStamp", timestamp);
                params.put("isBatch", isbatch);
                params.put("acceptType", accepttype);
                params.put("redirectUrl", "");
                params.put("assigner", assigner);
                params.put("isSMS", 1);
                params.put("toDoPcUrl", todopcurl);
                params.put("isPcDetail", ispcdetail);
                params.put("buttonValue", buttonvalue);
                params.put("languageEnv", languageenv);
                System.out.println("addOrUpToDo params:" + params);
                String res = HttpUtil.sendPostJsonWithHeader(addOrUpToDoUrl, params, sysid, syspwd, adduserid);
                System.out.println("addOrUpToDo response:" + res);
                JSONObject jsonObject2 = JSON.parseObject(res);
                if ("true".equals(jsonObject2.getString("success"))) {
                    System.out.println((new Date()) + ":addOrUpToDoH3C success");
                } else {
                    System.out.println((new Date()) + ":addOrUpToDoH3C fail:" + jsonObject2.getString("Msg"));
                }
            }
            return ResultUtil.success(null);
        } catch (Exception e) {
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }


    @Override
    public Result updateData(String data) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        JSONObject jsonObject = JSONObject.parseObject(data);
        String systemid = "IDCM";
        String workFlowId = jsonObject.getString("workFlowId");
        String docId = jsonObject.getString("docId");
//        String currentNodeId = jsonObject.getString("currentNodeId");
//        String applytime = StringUtils.isBlank(jsonObject.getString("applytime")) ? sdf.format(new Date()) : jsonObject.getString("applytime");
        String empNumber = jsonObject.getString("empNumber");
//        String buttonType = jsonObject.getString("buttonType");
//        String targetReceiver = jsonObject.getString("targetReceiver");//转发人：域账号
//        String opinion = jsonObject.getString("opinion");
//        String data1 = jsonObject.getString("data");//表单提交json串，批量办理时无该字段
//        MultipartFile files = null;
        String status = jsonObject.getString("status");
//        if (buttonType.equalsIgnoreCase("agree")) {
//            status = "APPROVED";
//        }
//        if (buttonType.equalsIgnoreCase("reject")) {
//            status = "WAITING";
//        }
        ApproveH3c approveH3c = new ApproveH3c();
        approveH3c.setSystemid(systemid);
        approveH3c.setDocunid(docId);
        approveH3c.setProcessid(workFlowId);
        approveH3c.setStatus(status);
        approveH3c.setTimestamp(sdf.format(new Date()));
        approveH3c.setAuthorname(empNumber);
        approveH3cDao.update(approveH3c);
        return null;
    }

    @Override
    public List<ApproveH3c> getList() {
        return null;
    }

    @Override
    public JSONObject getApproveList(String data) {
        JSONObject jsonObjectReq = JSON.parseObject(data);
        String systemid = jsonObjectReq.getString("systemId");
        String processid = jsonObjectReq.getString("workFlowId");
        String docunid = jsonObjectReq.getString("docId");
        String currentNodeId = jsonObjectReq.getString("currentNodeId");
        String empNumber = jsonObjectReq.getString("empNumber");
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isBlank(processid) || StringUtils.isBlank(docunid)) {
            jsonObject.put("currentEmpNumber", null);
            jsonObject.put("attachments", null);
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("taskLink", null);
            jsonObject.put("data", jsonObject2);
            jsonObject.put("historyInfos", null);
            jsonObject.put("success", false);
            jsonObject.put("message", "");
            return jsonObject;
        }

        ApproveH3c approveH3c = approveH3cDao.getById(systemid, processid, docunid);
        if (approveH3c == null) {
            jsonObject.put("currentEmpNumber", null);
            jsonObject.put("attachments", null);
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("taskLink", null);
            jsonObject.put("data", jsonObject2);
            jsonObject.put("historyInfos", null);
            jsonObject.put("success", false);
            jsonObject.put("message", "");
            return jsonObject;
        }
        jsonObject.put("currentEmpNumber", approveH3c.getAuthorid());
        jsonObject.put("attachments", null);
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("taskLink", approveH3c.getTodopcurl());
        jsonObject.put("data", jsonObject2);
        jsonObject.put("historyInfos", null);
        jsonObject.put("success", true);
        jsonObject.put("message", "");
        return jsonObject;
    }

    @Override
    public ApproveH3c getById(String systemid, String processid, String docunid) {
        return approveH3cDao.getById(systemid, processid, docunid);
    }

}
